//
//  FBUtilities.m
//  Radio Streaming
//
//  Created by HaiNguyenThanh on 4/19/11.
//  Copyright 2011 VinaSource. All rights reserved.
//

#import "FBUtilities.h"
#import "FBPoster.h"
#import "SettingHomeViewController.h"

@implementation FBUtilities
@synthesize fullName;
@synthesize loginDelegate;
@synthesize messageToPost;

- (id) init
{
	if (self == [super init])
	{
		fbSession = nil;
		
		fbSession = [FBSession
					 sessionForApplication:KFBAPPID
					 secret:KFBSECRETKEY
					 delegate:self];
		[fbSession retain];
		[fbSession resume];
	}
    return self;
}

- (void)getFacebookName
{
	NSString* fql = [NSString stringWithFormat:@"select name from user where uid == %lld", fbSession.uid];
	NSDictionary* params = [NSDictionary dictionaryWithObject:fql forKey:@"query"];
	[[FBRequest requestWithDelegate:self] call:@"facebook.fql.query" params:params];
}

- (void) facebookLogin:(id)delegate
{
	FBLoginDialog* dialog = [[FBLoginDialog alloc]initWithSession:[FBSession session]];
	if (delegate != nil)
	{
		dialog.delegate = self;
	}
	loginDelegate = delegate;
	[dialog show];
	[dialog release];
}

- (void) postToWall:(NSString*)message
{
	//[FBPoster PostToWall:message];
}

- (NSString*) getFullName
{
	return fullName;
}

#pragma mark -
#pragma mark FBSessionDelegate

/**
 * Called when a user has successfully logged in and begun a session.
 */
- (void)session:(FBSession*)session didLogin:(FBUID)uid
{
    NSLog(@"session:(FBSession*)session didLogin:(FBUID)uid");
	[self getFacebookName];
    [self.loginDelegate facebookFinishedLogin];
}

/**
 * Called when a user closes the login dialog without logging in.
 */
- (void)sessionDidNotLogin:(FBSession*)session{
    NSLog(@"sessionDidNotLogin");
}

/**
 * Called when a session is about to log out.
 */
- (void)session:(FBSession*)session willLogout:(FBUID)uid{
    NSLog(@"session:(FBSession*)session willLogout");
}

/**
 * Called when a session has logged out.
 */
- (void)sessionDidLogout:(FBSession*)session
{
	[loginDelegate logout];
}

#pragma mark End FBSessionDelegate
#pragma mark -
#pragma mark FBRequestDelegate method

/**
 * Called just before the request is sent to the server.
 */
- (void)requestLoading:(FBRequest*)request{
}

/**
 * Called when the server responds and begins to send back data.
 */
- (void)request:(FBRequest*)request didReceiveResponse:(NSURLResponse*)response{
}

/**
 * Called when an error prevents the request from completing successfully.
 */
- (void)request:(FBRequest*)request didFailWithError:(NSError*)error{
}

/**
 * Called when a request returns and its response has been parsed into an object.
 *
 * The resulting object may be a dictionary, an array, a string, or a number, depending
 * on thee format of the API response.
 */
- (void)request:(FBRequest*)request didLoad:(id)result{
	[loginDelegate getUserInfo:result];
}

/**
 * Called when the request was cancelled.
 */
- (void)requestWasCancelled:(FBRequest*)request
{
}

#pragma mark End

#pragma mark -
#pragma mark FBDialogDelegate Methods

/**
 * Called when the dialog succeeds and is about to be dismissed.
 */
- (void)dialogDidSucceed:(FBDialog*)dialog
{
    [self.loginDelegate facebookFinishedLogin];
    if(![viewDelegate isKindOfClass:[SettingHomeViewController class]])
	{
		//[viewDelegate createFBPostDialog];
	}
}

/**
 * Called when the dialog is cancelled and is about to be dismissed.
 */
- (void)dialogDidCancel:(FBDialog*)dialog{
}

/**
 * Called when dialog failed to load due to an error.
 */
- (void)dialog:(FBDialog*)dialog didFailWithError:(NSError*)error{
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" 
													message:NSLocalizedString(@"KCONNECT_FACEBOOK_ERROR",@"")
												   delegate:nil 
										  cancelButtonTitle:@"Close"
										  otherButtonTitles:nil];
	[alert show];
	[alert release];
}

/**
 * Asks if a link touched by a user should be opened in an external browser.
 *
 * If a user touches a link, the default behavior is to open the link in the Safari browser, 
 * which will cause your app to quit.  You may want to prevent this from happening, open the link
 * in your own internal browser, or perhaps warn the user that they are about to leave your app.
 * If so, implement this method on your delegate and return NO.  If you warn the user, you
 * should hold onto the URL and once you have received their acknowledgement open the URL yourself
 * using [[UIApplication sharedApplication] openURL:].
 */
- (BOOL)dialog:(FBDialog*)dialog shouldOpenURLInExternalBrowser:(NSURL*)url
{
	return YES;
}

#pragma mark -
- (void)dealloc
{
    self.fullName = nil;
    self.messageToPost = nil;
	[super dealloc];
}

@end
